const ARule = require('./ARule');
const RandomRule = require('./RandomRule');
const RoundRule = require('./RoundRule');
const StickyRule = require('./StickyRule');

/**
 * 服务节点选择策略：随机，轮流，粘连
 */
const SelectStrategy = Object.freeze({
    RANDOM: "random", //从可用服务节点中随机选择一个节点
    ROUND: "round", //在可用服务节点列表中按顺序轮流选择一个节点
    STICKY: "sticky", //选用在session中最近使用的一个节点
});

class RuleFactory {
    /**
     * 根据strategy参数返回指定的负载均衡规则，如果没有找到匹配strategy的实现类 默认使用随机规则
     * 
     * @returns ARule 负载均衡规则对象
     */
    static getRule(strategy) {
        console.debug(`RuleFactory strategy : ${strategy}`);
        if (SelectStrategy.RANDOM == strategy) {
            return new RandomRule();
        } else if (SelectStrategy.ROUND == strategy) {
            return new RoundRule();
        } else if (SelectStrategy.STICKY == strategy) {
            return new StickyRule();
        } else {
            return new RandomRule();
        }
    }

}


module.exports = RuleFactory;